/*测试数据*/
var testdata=[
	{
		id: 1,
        productId: 'asdssd',
        productName: 'linda1',
        oldStore: 231,
        addNum: 22,
        supplierPrice: 23.2,
        supplier: {
            id: 23,
            supplierName: '李波'
        },
		createTime: 1502937009
	},
    {
        id: 2,
        productId: 'asdssd2',
        productName: 'linda1',
        oldStore: 231,
        addNum: 22,
        supplierPrice: 23.2,
        supplier: {
            id: 23,
            supplierName: '李波'
        },
        createTime: 1502937009
    },
    {
        id: 3,
        productId: 'asdssd3',
        productName: 'linda1',
        oldStore: 231,
        addNum: 22,
        supplierPrice: 23.2,
        supplier: {
            id: 23,
            supplierName: '李波'
        },
        createTime: 1502937009
    },
];

$(function(){
    getBrandData();
    searchTableData();
	$("#searchBtn").on('click',function () {
        searchTableData();
    });
    $("#btnExcel").on('click',function () {
        excelsearchData();
    });
});
/*查询excel数据，并导出*/
function excelsearchData(){
    var keyword = $("#keyword").val();
    var brandSelect = $("#brandSelect").val();
    var theStartTme = $("#startTime").val();
    var theEndTime = $("#endTime").val();
    if(theStartTme!=null && theStartTme!=""){
        theStartTme = Date.parse(new Date(theStartTme));
        theStartTme = theStartTme;
    }else{
        theStartTme = 0;
    }
    if(theStartTme!=null && theStartTme!=""){
        theEndTime = Date.parse(new Date(theEndTime));
        theEndTime = theEndTime;
    }else{
        theEndTime = 0;
    }
    if(theStartTme!=0 && theEndTime!=0){
        var nday = daysBetween(theStartTme,theEndTime);
        if(nday<=0){
            consoleMes($("#manageContent"),2000,"warning","开始时间和结束时间至少相差一天！");//success、info、warning、danger
            return;
        }
    }
    var productId=getUrlParam("tgid") || '';
    var params = {
        queryKeyword: keyword,
        productBrand: brandSelect,
        startTime: theStartTme,
        endTime: theEndTime,
        productId: productId
    };
    $.post(
        gobalServer+"instock/downloadInStockByQueryCondition",
        params,
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","正在导出excel文件！");
                window.open(gobalDownUrl+backData.msg);
            }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
    );
};
/*获取品牌数据并填充*/
function getBrandData(){
    $.post(
        gobalServer+"productBrand/selectAll",
        {},
        function(backData){
            $("#brandSelect").empty();
            var option = $("<option>").attr({"value":''}).text("品牌查询");
            $("#brandSelect").append(option);
            if(backData.statusCode==200){
                $.each(backData.commonList,function (index,item) {
                    var option = $("<option>").attr({"value":item.id}).text(item.brandName);
                    $("#brandSelect").append(option);
                })
            }else{
                consoleMes($("#manageContent"),2000,"warning","无品牌数据，你可以添加相关品牌！");//success、info、warning、danger

            }
        }
    );
};
/*查询数据*/
function searchTableData(){

    //consoleMes($("#manageContent"),2000,"warning","正在查询!");
    /*var backData=testdata;//[];
     tableInit(backData);*/

    var keyword = $("#keyword").val();
    var brandSelect = $("#brandSelect").val();
    var theStartTme = $("#startTime").val();
    var theEndTime = $("#endTime").val();
    if(theStartTme!=null && theStartTme!=""){
        theStartTme = Date.parse(new Date(theStartTme));
        theStartTme = theStartTme;
    }else{
        theStartTme = 0;
    }
    if(theStartTme!=null && theStartTme!=""){
        theEndTime = Date.parse(new Date(theEndTime));
        theEndTime = theEndTime;
    }else{
        theEndTime = 0;
    }
    if(theStartTme!=0 && theEndTime!=0){
        var nday = daysBetween(theStartTme,theEndTime);
        if(nday<=0){
            consoleMes($("#manageContent"),2000,"warning","开始时间和结束时间至少相差一天！");//success、info、warning、danger
            return;
        }
    }
    var productId=getUrlParam("tgid") || '';
    var params = {
        queryKeyword: keyword,
        productBrand: brandSelect,
        startTime: theStartTme,
        endTime: theEndTime,
        productId: productId
    };

    $.post(
         gobalServer+"instock/query",
        params,
         function(backData){
             if(backData.statusCode==200){
                 var backData=backData.pageBean.list;//[];
                 tableInit(backData);
             }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

             }
         }
     );
};
/*获取表格数据*/
function getTableData(){
	var backData=testdata;//[];
	tableInit(backData);
    var tgid=getUrlParam("tgid");
    /*if(tgid!=null && tgid>0) {
        $.post(
            gobalServer + "productBrand/selectAll",
            {},
            function (backData) {
                if (backData.statusCode == 200) {
                    var backData = backData.commonList;//[];
                    tableInit(backData);
                } else {
                    consoleMes($("#manageContent"), 2000, "warning", backData.msg);//success、info、warning、danger

                }
            }
        );
    }else{
        $.post(
            gobalServer + "productBrand/selectAll",
            {},
            function (backData) {
                if (backData.statusCode == 200) {
                    var backData = backData.commonList;//[];
                    tableInit(backData);
                } else {
                    consoleMes($("#manageContent"), 2000, "warning", backData.msg);//success、info、warning、danger

                }
            }
        );
    }*/
};
/*表格初始化*/
function tableInit(backData){
	var objectArray=[];
    if(backData.length>0){
        objectArray=dataToJson(backData);
    }
    $('#stockTable').bootstrapTable('destroy').bootstrapTable({
        toolbar: '#toolbar',                //工具按钮用哪个容器
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        data: objectArray,
        pageList: [5, 10, 20, 40, 100, 200]/*,
        rowStyle: function rowStyle(row,index) {
            if(row.mark!=null && row.mark>0){
                return {
                    css:{
                        "background-color": "#ffc930",
                        "color": "#fff"
                    }
                };
            }else{
                return {
                    css:{

                    }
                };
            }
        }*/
    });
};
/*表格数据封装*/
function dataToJson(dataResult) {
    var objectArray=new Array();
    $.each(dataResult,function(index,item){
    	var createTime="--";
    	if(item.createTime!=null){
        	createTime=formatDateTimeYMDHMS(item.createTime);
       }
       var supplier = "--";
    	if(item.supplier!=null){
            supplier="<a class='btn-a-link' href='"+gobalWeb+"product/suppliershow.html?tgid="+item.supplier.id+"' >"+item.supplier.supplierName+"</a>";
        }
        var inStockPrice="";
        if(item.inStockPrice!=null){
            inStockPrice="<b class='text-red'>￥"+item.inStockPrice+"</a>";
        }
        var operate="";
        operate="<a class='btn-a-common' href='"+gobalWeb+"product/storeedit.html?tgid="+item.id+"' >修改</a>";
        operate+="<a class='btn-a-common' href='javascript:;' onclick='deleteItemData("+item.id+")'>删除</a>";
        var arr={
        	"id":item.id,
        	"productId":item.productId,
            "productName":item.productName,
            "productBrand":item.productBrand,
            "productVersion":item.productVersion,
            "oldStore":item.originalNumber,
            "addNum":item.addNumber,
            "nowStore": item.presentNumber,
            "supplierPrice": inStockPrice,
            "supplier":item.supplier,
            "createTime":createTime,
            "createTimesort":item.createTime,
            "operate":operate
        };
        objectArray.push(arr);
    });
    return objectArray;
};
/*删除数据*/
function deleteItemData(tgid){
	if(tgid!=null && tgid>0){
		var msg = "您真的确定要删除吗？\n\n删除后无法撤回！";
		if (confirm(msg)==true){
			/*consoleMes($("#manageContent"),2000,"success","数据已经成功删除！");
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);*/
            $.post(
                gobalServer+"instock/delete",
                {"id":tgid},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success","库存信息删除成功！");
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

                    }
                }
            );

		}
	}else{
		consoleMes($("#manageContent"),2000,"warning","删除失败，请刷新页面重试！");//success、info、warning、danger
	}
};
